﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;


namespace Bradaz.OpenSource.CustomValidation
{
    /// <summary>
    /// This class allows me to build code to validate my what type of Objects im using.
    /// That way i can build specfic logic to cope with this
    /// </summary>
    public class ObjectValidation
    {

        public static bool IsNumericType(Type type)
        {
            if (type == null)
            {
                return false;
            }

            switch (Type.GetTypeCode(type))
            {
                case TypeCode.Byte:
                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.SByte:
                case TypeCode.Single:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    return true;

                case TypeCode.Object:
                    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        return IsNumericType(Nullable.GetUnderlyingType(type));
                    }
                    return false;

            }

            return false;
        }

        public static bool IsStringType(Type type)
        {
            if (type == null)
            {
                return false;
            }

            switch (Type.GetTypeCode(type))
            {
                case TypeCode.String:
                    return true;

                case TypeCode.Object:
                    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        return IsStringType(Nullable.GetUnderlyingType(type));
                    }

                    return false;

            }

            return false;
        }
    }
}
